#!/usr/bin/env python
'''
soiflatcombine creates a [normalized] master flat-field for the SOI CCD.
Usage: soiflatcombine
'''

from pyraf import iraf
from iraf import pysoar 

from pyraf.irafglobals import yes,no
import soarprint
import pysoarlib as lib

import sys,os
import numpy as np

def donormalize(flat,nflat):
    iraf.imextensions(flat,Stdout="flatext")
    amp_values = iraf.imstat(images='@flatext',fields="mean",format="no",Stdout=1)
    mean_value = np.mean(np.array(amp_values,float))
    iraf.mscred(_doprint=0)
    lib.delete_if_exists(nflat)
    iraf.mscarith(operand1=flat,op="/",operand2=mean_value,result=nflat)

def clean_tmp_files():
        lib.delete_if_exists('flatext','tmpin','tmpout', 'tmpflat.fits')
        
def soiflatcombine(images,\
               output,    \
               normalize,\
               verbose,  \
               process,  \
               xtalkcor, \
               fixpix,   \
               overscan, \
               trim,     \
               zerocor,  \
               darkcor,  \
               flatcor,  \
               sflatcor, \
               fixfile,  \
               biassec,  \
               trimsec,  \
               zero,     \
               dark,     \
               flat,     \
               sflat,    \
               minreplace):

        iraf.mscred(_doprint=0)

        iraf.ccdproc.setParam('verbose',verbose)
        iraf.ccdproc.setParam('xtalkcor',xtalkcor)
        iraf.ccdproc.setParam('fixpix',fixpix)
        iraf.ccdproc.setParam('overscan',overscan)
        iraf.ccdproc.setParam('trim',trim)
        iraf.ccdproc.setParam('ccdtype','')
        iraf.ccdproc.setParam('merge','no')
        iraf.ccdproc.setParam('zerocor',zerocor)
        iraf.ccdproc.setParam('darkcor',darkcor)
        iraf.ccdproc.setParam('flatcor',flatcor)
        iraf.ccdproc.setParam('sflatcor',sflatcor)
        iraf.ccdproc.setParam('fixfile',fixfile)
        iraf.ccdproc.setParam('biassec',biassec)
        iraf.ccdproc.setParam('trimsec',trimsec)
        iraf.ccdproc.setParam('zero',zero)
        iraf.ccdproc.setParam('dark',dark)
        iraf.ccdproc.setParam('flat',flat)
        iraf.ccdproc.setParam('sflat',sflat)
        iraf.ccdproc.setParam('minreplace',minreplace)
        
        #Cleaning previous tmp files.
        clean_tmp_files()
        
        #creating the tmp file list of images.
        iraf.files(images,Stdout='tmpin')
        iraf.files('tmp_//@tmpin',Stdout='tmpout')
        
        #starting the ccdproc on the flat images
        if process:
                iraf.ccdproc.setParam('output','@tmpout')
                iraf.ccdproc.setParam('images','@tmpin')
                iraf.ccdproc(mode='h',Stdout=1)
                tmpflat = '@tmpout'
        else:
                tmpflat = '@tmpin'
        
        #starting the flatcombine on the ccdproc flat frames.
        iraf.flatcombine(input=tmpflat,output='tmpflat.fits',ccdtype="",combine='median',reject="avsigclip",scale = "mode",process='no',subsets='no')
        #calling the function that will normalize the flat
        if normalize:      
                donormalize('tmpflat.fits',output)
        else:
                iraf.imcopy('tmpflat.fits',output)

        #deleting tmp files
        iraf.imdelete('@tmpout')
        clean_tmp_files()

parfile = iraf.osfn("pysoar$soi/soiflatcombine.par")
t = iraf.IrafTaskFactory(taskname="soiflatcombine",value=parfile,function=soiflatcombine)
